#include "stdafx.h"
#include "City.h"
#include "config.h"
#include "Scene.h"
#include "ObjectManager.h"

bool sgCity::m_creatorRegisted = CObjectManager::Instance().RegisteCreator(OBJ_CITY, sgCity::Creator );

sgCity::sgCity( int type /*= 0 */ )
{
    m_type = type;
    m_classType = OBJ_CITY;
}

sgCity::~sgCity()
{

}

bool sgCity::Initialize()
{
    bool ret = sgEntity::Initialize();
    m_layer = eLayer_City;
    const CityEntry *entry = Config::Instance().GetCityEntry( m_type );
    CHECK_PTR(entry, false);
    m_entry = *entry;
    m_animation.Initialize( MBToWChar(entry->animName.c_str()) );
    m_animation.StartAnim( L"Stand" );
    return ret;
}

CityEntry & sgCity::GetEntry()
{
    return m_entry;
}

sgCity * sgCity::Clone()
{
    sgCity *ret = new sgCity( m_type );
    ret->Initialize();
    ret->SetPosition( m_position );
    ret->m_entry = m_entry;
    ret->SetOwner( m_owner );
    return ret;
}

void sgCity::ProcessMessage( sgMessage *msg )
{
    switch(msg->type)
    {
    case MSG_SELECTED:
        {
            sgMessage *m = new sgMessage;
            m->from = this;
            m->type = MSG_GUI_CITY_SELECTED;
            CObjectManager::Instance().SendMessageToGUI( m );
        }
        break;
    }
    return sgEntity::ProcessMessage( msg );
}

sgObject * sgCity::Creator()
{
    return new sgCity;
}

void sgCity::Serialize( CSerialize& s )
{
    sgForceEntity::Serialize(s);
    s&MAKE_PAIR(m_type,"type");
    s&MAKE_PAIR(m_entry.defense,"defense");
    s&MAKE_PAIR(m_entry.hp,"health");
    m_layer = eLayer_City;
    m_animation.Initialize( MBToWChar(m_entry.animName.c_str()) );
    m_animation.StartAnim( L"Stand" );
}
